package com.whu.tcx01.utils;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class PointUtil {

    //计算垂足
    //x,y轨迹点
    //lx1,2 ly1,2是线两个端点经纬度


    public static void Foot(double x, double y, double lx1, double ly1, double lx2, double ly2) {

//        System.out.println(x+" "+y+" "+lx1+" "+ly1+" "+ lx2 +" "+ ly2);

        double footx,footy;//垂足经纬度

        double dx = lx1-lx2;
        double dy = ly2-ly1;


        double u = (x - lx1) * dx + (y - ly1) * dy;
        u/=dx*dx + dy*dy;

        footx = lx1 - u * dx;
        footy = ly1 + u * dy;

        double d = Math.abs((lx1 - lx2) * (lx1 - lx2) + (ly1 - ly2) * (lx1 - ly2));
        double d1 = Math.abs((lx1 - footx) * (lx1 - footx) + (ly1 - footy) * (lx1 - footy));
        double d2 = Math.abs((lx2 - footx) * (lx2 - footx) + (ly2 - footy) * (lx2 - footy));


        if(d1 > d || d2 > d){
            if(d1 > d2){
                System.out.println("垂足超出线段，选右端点，即：" + lx2 + " " + ly2);
                return;
            } else{
                System.out.println("垂足超出线段，选左端点，即：" + lx1 + " " + ly1);
                return;
            }
        }else {
            System.out.println("垂足在线段上，即：" + footx + " " + footy);
        }
    }


    public static Double[] Foot1(double x, double y, double lx1, double ly1, double lx2, double ly2) {
        Double[] coords = new Double[2];
        double footx,footy;//垂足经纬度

        double dx = lx1-lx2;
        double dy = ly2-ly1;


        double u = (x - lx1) * dx + (y - ly1) * dy;
        u/=dx*dx + dy*dy;

        footx = lx1 - u * dx;
        footy = ly1 + u * dy;

        double d = Math.abs((lx1 - lx2) * (lx1 - lx2) + (ly1 - ly2) * (lx1 - ly2));
        double d1 = Math.abs((lx1 - footx) * (lx1 - footx) + (ly1 - footy) * (lx1 - footy));
        double d2 = Math.abs((lx2 - footx) * (lx2 - footx) + (ly2 - footy) * (lx2 - footy));


        if(d1 > d || d2 > d) {
            if (d1 > d2) {
//                System.out.println("垂足超出线段，选右端点，即：" + lx2 + " " + ly2);
                coords[0] = lx2;
                coords[1] = ly2;
                return coords;
            } else {
//                System.out.println("垂足超出线段，选左端点，即：" + lx1 + " " + ly1);
                coords[0] = lx1;
                coords[1] = ly1;
                return coords;
            }
        }
//        System.out.println("垂足在线段上，即：" + footx + " " + footy);
        coords[0] = footx;
        coords[1] = footy;

//        System.out.println(x+" "+y+" "+lx1+" "+ly1+" "+ lx2 +" "+ ly2 + "垂足点：" +coords[0]+","+coords[1] );
        return coords;
    }

    //计算点到垂距距离
    public static double Distance(double x, double y, Double[] coords) {

        double d = CoorTrans.GetDistance(x,y,coords[0],coords[1]);
//        System.out.println(d);
        return d;
    }

    //计算轨迹点之间的距离
    public static List<Double> CalTrackPointDistance(Double[] xArr,Double[] yArr){
        List<Double> disList = new ArrayList<>();//记录距离
        for (int i = 1; i < xArr.length ; i++) {
            disList.add(CoorTrans.GetDistance(xArr[i - 1],yArr[i - 1],xArr[i],yArr[i - 1]));
//            disList.add(Math.sqrt((xArr[i - 1] - xArr[i]) * (xArr[i - 1] - xArr[i]) + (yArr[i - 1] - yArr[i]) * (yArr[i - 1] - yArr[i])));
            System.out.println(disList.get(i-1));
        }
        return disList;
    }

    //轨迹点之间的距离
    public static List<Double> getCarDistance(List<String> coorList) {
        Double[] xArr = new Double[coorList.size()];
        Double[] yArr = new Double[coorList.size()];
        List<Double> disList = new ArrayList<>();

        for (int i = 0; i < coorList.size(); i++) {
            String str = coorList.get(i);
            String[] strArr = str.split(",");
            xArr[i]= Double.valueOf(strArr[0]);
            yArr[i]= Double.valueOf(strArr[1]);
//            System.out.println( xArr[i]+ " " +  yArr[i]);
        }
        disList = CalTrackPointDistance(xArr,yArr);
        return disList;
    }

    //计算相邻轨迹点之间的候选点之间的距离集合
    public static List<List<Double>> getCandDistance(List<List<String>> candListList) {
        List<List<Double>> cDisList = new ArrayList<>();
        int f = 0;
        for (int i = 0; i < candListList.size()-1; i++) {
            List<Double> cDis = new ArrayList<>();//存放距离
            List<String> canList1 = candListList.get(i);
            List<String> canList2 = candListList.get(i+1);
            for (int j = 0; j < canList1.size(); j++) {
                String[] canArr1 = canList1.get(j).split(";");
                for (int k = 0; k < canList2.size(); k++) {
                    String[] canArr2 = canList2.get(k).split(";");

                    double x1 = Double.parseDouble(canArr1[0]);
                    double y1 = Double.parseDouble(canArr1[1]);
                    double x2 = Double.parseDouble(canArr2[0]);
                    double y2 = Double.parseDouble(canArr2[1]);

                    cDis.add(CoorTrans.GetDistance(x1,y1,x2,y2));
                }
            }
            cDisList.add(cDis);
        }
        System.out.println(cDisList.size());
        return cDisList;
    }

    public static void remove(List<Double> minDisProb, int i) {
        Iterator<Double> it = minDisProb.iterator();
        while(it.hasNext())
        {
            if(it.next() == i){
                it.remove();
            }
        }
    }


    public static Double[] Foot2(double x, double y, double lx1, double ly1, double lx2, double ly2) {


        //计算点线关系函数
        double r = getR(x, y, lx1, ly1, lx2, ly2);
//        System.out.println("点线关系函数:" + r);

        Double[] coords = new Double[2];

        double fx = lx1 + r * (lx2 - lx1);
        double fy = ly1 + r * (ly2 - ly1);

        if(r > 1){
            coords[0] = lx2;
            coords[1] = ly2;
            return coords;
        }else if(r < 0){
            coords[0] = lx1;
            coords[1] = ly1;
            return coords;
        }else {
            coords[0] = fx;
            coords[1] = fy;
            return coords;
        }
    }

/*    public static Double[] Foot3(double x, double y, double lx1, double ly1, double lx2, double ly2) {

        //计算点线关系函数
        double r = getR(x, y, lx1, ly1, lx2, ly2);
//        System.out.println("点线关系函数:" + r);

        Double[] coords = new Double[2];

        double k1 = (ly1-ly2)/(lx1-lx2);
        double k2 = -1/k1;
        double fx = (x+k1*y+k1*k1*lx1-k1*ly1)/(k1+1);
        double fy = ();

        if(r > 1){
            coords[0] = lx1;
            coords[1] = ly1;
            return coords;
        }else if(r < 0){
            coords[0] = lx2;
            coords[1] = ly2;
            return coords;
        }else {
            coords[0] = fx;
            coords[1] = fy;
            return coords;
        }
    }*/

    private static double getR(double x, double y, double lx1, double ly1, double lx2, double ly2) {
        double r = ((y-ly1)*(ly2-ly1) + (x-lx1)*(lx2-lx1))/((lx2-lx1)*(lx2-lx1)+(ly2-ly1)*(ly2-ly1));
//        double r = ((y-ly1)*(ly2-ly1) + (x-lx1)*(lx2-lx1))/((lx1-lx2)*(lx1-lx2)+(ly1-ly2)*(ly1-ly2));
        return r;
    }

}
